package com.braveman.im.ui.activity.message;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.braveman.im.R;
import com.braveman.im.constant.Constants;
import com.braveman.im.ui.activity.PersonInfoDetailsActivirt;
import com.braveman.im.ui.fragment.MessageForChatFragment;
import com.braveman.im.utils.UtilsStyle;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.widget.TitleBarView;
import com.ht.baselibs.utils.ActivityUtils;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.widget.EaseChatMessageList;
import com.vondear.rxtools.RxBarTool;


public class ChatAllActivity extends BaseActivity {
    public static ChatAllActivity activityInstance;
    private MessageForChatFragment chatFragment;
    private MessageForChatFragment messageForChatFragment;
    String toChatUsername;
    private EaseChatMessageList messageList;
    private String title;
    private String usrId;
    private int states;//消息类型
    private String groupId;
    private String usrName;
    private String imFriendsId;


    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {

        mTitleBarView.setTitleText(usrName + "").setLeftBtnIcon(R.mipmap.back_black).setOnLeftBtnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtils.finishActivity(ChatAllActivity.this);
            }
        });
        mTitleBarView.addActions(new TitleBarView.Action(R.mipmap.title_icon_more) {
            @Override
            public void performAction(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("user_id", usrId);
                switch (states) {
                    case 1://群
                        bundle.putString("groupId", groupId);
                        ActivityUtils.startActivity(ChatAllActivity.this, GroupInfoActivity.class, bundle);
                        break;
                    case 2://单位
                        ActivityUtils.startActivity(ChatAllActivity.this, CompanyInfoActivity.class, bundle);
                        break;
                    case 3://单聊
                        bundle.putString("imFriendsId", imFriendsId);
                        ActivityUtils.startActivity(ChatAllActivity.this, PersonInfoDetailsActivirt.class, bundle);
                        break;
                }
            }
        });
    }

    @Override
    protected int initContentView() {
        UtilsStyle.setStatusBarMode(this, true);
        RxBarTool.FlymeSetStatusBarLightMode(getWindow(), true);
        RxBarTool.MIUISetStatusBarLightMode(getWindow(), true);
        return R.layout.activity_all_chat;
    }

    protected void initIntentData(Intent callIntent) {
        states = callIntent.getIntExtra("states", -1);
        groupId = callIntent.getStringExtra("groupId");
        usrName = callIntent.getStringExtra("usrName");
        usrId = callIntent.getStringExtra("user_id");

        imFriendsId = callIntent.getStringExtra("imFriendsId");
    }

    @Override
    protected void initChildView() {
        activityInstance = this;
        //user or group id
//        toChatUsername = getIntent().getExtras().getString(EaseConstant.EXTRA_USER_ID);
        chatFragment = new MessageForChatFragment();

        //set arguments
        Bundle args = new Bundle();
        switch (states) {
            case 1://群
                args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
                args.putString(EaseConstant.EXTRA_USER_ID, usrId);
                args.putString(EaseConstant.EXTRA_USER_ID_TYPE, Constants.GROUP);
                args.putString(EaseConstant.EXTRA_USER_NAME, usrName);

                break;
            case 2://单位
                args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);
                args.putString(EaseConstant.EXTRA_USER_ID, usrId);
                args.putString(EaseConstant.EXTRA_USER_ID_TYPE, Constants.COMPANY);
                args.putString(EaseConstant.EXTRA_USER_NAME, usrName);
                break;
            case 3://单聊
                args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
                args.putString(EaseConstant.EXTRA_USER_ID, usrId);
                args.putString(EaseConstant.EXTRA_USER_NAME, usrName);
                break;

        }


        chatFragment.setArguments(args);
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();

    }

    @Override
    protected void initChildData() {
    }

    @Override
    protected void setChildViewListener() {

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // enter to chat activity when click notification bar, here make sure only one chat activiy
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }

    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }

    public String getToChatUsername() {
        return toChatUsername;
    }

}
